<?php
/**
 * 后台公共类
 * @author 肖小平
 *
 */
class BaseAction extends Action {
	public $permission;//存放权限
	/*
	 * 构造方法：检查是否登录，导航设置，面包屑设置，权限检查
	 */
	public function _initialize(){
		$this->check_login();
		$this->check_permission();
		$this->header();
		$this->location();
	}
	/**
	 * 设置面包屑方法
	 */
	private function location(){
		$where['action'] = $Think.MODULE_NAME;
		$loaction = M('AdminMenu')->where($where)->find();
		if($loaction){
			$where1['id'] = $loaction['p_id'];
			$loaction_p = M('AdminMenu')->where($where1)->find();
		}
		
		$this->assign("loaction",$loaction);
		$this->assign("loaction_p",$loaction_p);
	}
	
	/**
	 * 登录检查
	 */
	public function check_login(){
		if (session('is_login') != 1){
			header("Location:".U('Login/login'));exit;
		}
	}
	/*
	 * 设置导航方法
	 * 
	 */
	private function header(){
		//排序方法,数值越小越靠前
		function menu_sort($a, $b) {
			$al = $a['sort'];
			$bl = $b['sort'];
			if ($al == $bl)
				return 0;
			return ($al > $bl) ? 1 : -1;
		}
		if ($this->permission == 'all'){
			$menus = M('AdminMenu')->select();
		}else{
			$menus = M('AdminMenu')->where("id in ($this->permission) or p_id=0")->select();
		}
		$menu_arr = array();
		foreach ($menus as $m) {
			if ($m['p_id']==0){
				$menu_arr[] = $m;
			}
			usort($menu_arr, 'menu_sort');//排序
		}
		foreach ($menu_arr as $k=>$m_arr){
			foreach ($menus as $m) {
				if ($m['p_id']==$m_arr['id']){
					$menu_arr[$k]['sub_menu'][] = $m;
				}
			}
			usort($menu_arr[$k]['sub_menu'], 'menu_sort');//排序
		}
		$this->assign('menu_arr',$menu_arr);
	}
	/**
	 * 权限检查
	 */
	public function check_permission(){
		$id = session('admin_id');
		$obj = M('Admin')->where("id=$id")->find();
		$where['id'] = $obj['role_id'];
		$per = M('AdminRole')->where($where)->find();
		$this->permission = $per['menu_ids'];
		if($per['menu_ids'] != 'all'){
			$action = $Think.MODULE_NAME.'-'.$Think.ACTION_NAME;//获取菜单方法
			$am = M('AdminMenu')->where("action='".$Think.MODULE_NAME."' and function='".$Think.ACTION_NAME."'")->find();
			if ($am && !in_array($am['id'],explode(',',$per['menu_ids']))){//判断是否拥有菜单权限
				echo '没有权限';exit;
			}
		}
	}
	/**
	 * 图片上传
	 * 成功返回$info,失败返回错误信息
	 * @param $path
	 */
	public function upload(){
		import('ORG.Net.UploadFile');
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 3145728 ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg','mp4', 'swf','flv');// 设置附件上传类型
		$upload->savePath =  './Public/Uploads/'.date('Y-m-d',time()).'/';// 设置附件上传目录
		if(!$upload->upload()) {// 上传错误提示错误信息
			$this->error($upload->getErrorMsg());
		}else{// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
		}
		return $info;
	}
	
	/**
	 * 可以不选择图片的上传方法
	 * 成功返回$info,失败返回错误信息
	 * 不选择图片返回false
	 * 
	 */
	public function upload_(){
		import('ORG.Net.UploadFile');
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 3145728 ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg','mp4', 'swf','flv');// 设置附件上传类型
		$upload->savePath =  './Public/Uploads/'.date('Y-m-d',time()).'/';// 设置附件上传目录
		if(!$upload->upload()) {// 上传错误提示错误信息
			return false;//不选择图片返回false
		}else{
			$info =  $upload->getUploadFileInfo();
		}
		return $info;
	}
	/**
	 * 图片视频
	 * 成功返回$info,失败返回错误信息
	 * @param $path
	 */
	public function video_upload(){
		import('ORG.Net.UploadFile');
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 3145728000 ;// 设置附件上传大小
		$upload->allowExts  = array('mp4');// 设置附件上传类型
		$upload->savePath =  './Public/Uploads/video/'.date('Y-m-d',time()).'/';// 设置附件上传目录
		if(!$upload->upload()) {// 上传错误提示错误信息
			$this->error($upload->getErrorMsg());
		}else{// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
		}
		return $info;
	}
	/**
	 * 可以不选择视频的上传方法
	 * 成功返回$info，
	 * 不选择图片返回false
	 * 
	 */
	public function video_upload_(){
		import('ORG.Net.UploadFile');
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 3145728000 ;// 设置附件上传大小
		$upload->allowExts  = array('mp4');// 设置附件上传类型
		$upload->savePath =  './Public/Uploads/video/'.date('Y-m-d',time()).'/';// 设置附件上传目录
		if(!$upload->upload()) {// 上传错误提示错误信息
			return false;
		}else{// 上传成功 获取上传文件信息
			$info =  $upload->getUploadFileInfo();
		}
		return $info;
	}
}
